﻿/* =========================================================================================
 * Copyright (C) 2014 Rajdeep Podder
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *  
 * ========================================================================================= */

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web.Mvc;
using System.Web;
using System.Web.UI;
using System.IO;
using ReCaptcha.Mvc5.Model;

namespace ReCaptcha.Mvc5
{
    /// <summary>
    /// MVC extension class for ReCaptcha 
    /// </summary>
    public static class ReCaptchaExtension
    {
        /// <summary>
        /// Extension method for HtmlHelper. usage @Html.reCAPTCHA(your_private_key)
        /// cref ="https://developers.google.com/recaptcha/docs/customization"
        /// </summary>
        /// <param name="htmlhelper"></param>
        /// <param name="publickey">Public key for recaptcha cref = "https://www.google.com/recaptcha/admin/create"</param>        
        /// <param name="theme">Recaptcha theme. Default is red.</param>
        /// <param name="lang">Language code for recptcha ui. Default is "en".</param>
        /// <param name="custom_translations">Map for custom translation string.</param>
        /// <param name="custom_theme_widget">Hrml element id if you are using custom theme.
        /// cref = "https://developers.google.com/recaptcha/docs/customization#Custom_Theming"
        /// </param>
        /// <param name="tabindex">Tab index of the user response textbox.</param>        
        /// <returns>Html for recaptcha.</returns>
        public static IHtmlString reCAPTCHA(
           this HtmlHelper htmlhelper,
           string publickey,
           string theme = ReCaptchaThemes.red,
           string lang = "en",
           IDictionary<string, string> custom_translations = null,
           string custom_theme_widget = null,
           int tabindex = 0)
        {
            bool secure = 
                HttpContext.Current.Request.Url.AbsoluteUri.ToString().StartsWith("https");

            HtmlTextWriter writer = new HtmlTextWriter(new StringWriter());
            writer.Write(reCAPTCHAApi.reCAPTCHAHtmlString(
                publickey,
                secure,
                theme,
                lang, 
                custom_translations, 
                custom_theme_widget, 
                tabindex).ToString());

            return htmlhelper.Raw(writer.InnerWriter.ToString());
        }

        /// <summary>
        /// Extension method for HtmlHelper. usage @Html.reCAPTCHA(your_private_key)
        /// cref ="https://developers.google.com/recaptcha/docs/customization"
        /// </summary>
        /// <param name="htmlhelper"></param>
        /// <param name="publickey">Public key for recaptcha cref = "https://www.google.com/recaptcha/admin/create"</param>
        /// <param name="customize_model">UI Customization model</param>
        /// <returns></returns>
        public static IHtmlString reCAPTCHA(
           this HtmlHelper htmlhelper,
           string publickey,
           ReCaptchaUIModel customize_model)
        {
            bool secure =
                HttpContext.Current.Request.Url.AbsoluteUri.ToString().StartsWith("https");

            HtmlTextWriter writer = new HtmlTextWriter(new StringWriter());
            writer.Write(reCAPTCHAApi.reCAPTCHAHtmlString(
                publickey,
                secure,
                customize_model.theme,
                customize_model.lang,
                customize_model.custom_translations,
                customize_model.custom_theme_widget,
                customize_model.tabindex).ToString());

            return htmlhelper.Raw(writer.InnerWriter.ToString());
        }

        /// <summary>
        /// Extension method for Controller. usage this.verifyReCAPTCHA(model, your_private_key, true)
        /// cref ="https://developers.google.com/recaptcha/docs/customization"
        /// </summary>
        /// <param name="controller">Controller</param>
        /// <param name="model">Model containating form data</param>
        /// <param name="privatekey">Recaptcha private key cref = "https://www.google.com/recaptcha/admin/create "</param>
        /// <param name="secure">true if using https</param>
        /// <returns>ReCaptcha server response</returns>
        public static async Task<ReCaptchaResponse> verifyReCAPTCHA(this Controller controller,
            ReCaptchaViewModel model,
            String privatekey,
            bool secure = false)
        {
            return await reCAPTCHAApi.verify(controller.Request.UserHostAddress, 
                model, 
                privatekey, 
                secure);
        }
    }
}
